package com.quick.develop.form.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.service.impl.BaseServiceImpl;
import com.quick.develop.form.dao.GenDynamicFormGroupDao;
import com.quick.develop.form.entity.GenDynamicFormGroupEntity;
import com.quick.develop.form.query.GenDynamicFormGroupQuery;
import com.quick.develop.form.vo.FormGroupVO;
import com.quick.develop.form.vo.GenDynamicFormGroupVO;
import com.quick.develop.form.convert.GenDynamicFormGroupConvert;
import com.quick.develop.form.service.GenDynamicFormFieldService;
import com.quick.develop.form.service.GenDynamicFormGroupService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 表单组
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2023-09-30
 */
@Service(value = "GenDynamicFormGroupServiceImplForm")
@AllArgsConstructor
public class GenDynamicFormGroupServiceImpl extends BaseServiceImpl<GenDynamicFormGroupDao, GenDynamicFormGroupEntity> implements GenDynamicFormGroupService {

    @Resource
    private GenDynamicFormFieldService dynamicFormFieldService;

    @Override
    public PageResult<GenDynamicFormGroupVO> page(GenDynamicFormGroupQuery query) {
        IPage<GenDynamicFormGroupEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(GenDynamicFormGroupConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<GenDynamicFormGroupEntity> getWrapper(GenDynamicFormGroupQuery query) {
        LambdaQueryWrapper<GenDynamicFormGroupEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(GenDynamicFormGroupVO vo) {
        GenDynamicFormGroupEntity entity = GenDynamicFormGroupConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(GenDynamicFormGroupVO vo) {
        GenDynamicFormGroupEntity entity = GenDynamicFormGroupConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<FormGroupVO> getFormGroupsByFormId(Integer id) {

        LambdaQueryWrapper<GenDynamicFormGroupEntity> wrapper = Wrappers.lambdaQuery(GenDynamicFormGroupEntity.class)
                .eq(GenDynamicFormGroupEntity::getFormId, id);

        return Optional.ofNullable(list(wrapper))
                .orElse(new ArrayList<>()).stream()
                .map(GenDynamicFormGroupConvert.INSTANCE::convertFormVo)
                .peek(group -> group.setElements(dynamicFormFieldService.getFormElementsByGroupId(group.getId())))
                .collect(Collectors.toList());
    }

}